package com.xfcycle.zebrabikegp.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.assist.BlueToothAssist;
import com.xfcycle.zebrabikegp.constant.BluetoothDeviceConstant;
import com.xfcycle.zebrabikegp.constant.ToastConstant;
import com.xfcycle.zebrabikegp.util.HandleDataByBLEUtil;
import com.xfcycle.zebrabikegp.util.HandleDataBySharePreferencesUtil;

/**
 * 自行车自动休眠时间设置
 * <p>
 * Created by xFcycle on 2017/5/19.
 */

public class BsSleepTimeActivity extends RootActivity {
    private static final String TAG = "AdjustBikeDormancy";

    private ImageView iv_adjust_sleep_time_back;

    private TextView tv_sleep_time_lock_percent;
    private SeekBar sb_sleep_time_lock;
    private LinearLayout ll_sleep_time_sleep;
    private TextView tv_sleep_time_sleep_percent;
    private SeekBar sb_sleep_time_sleep;

    private HandleDataByBLEUtil handleDateByBleUtil;
    private HandleDataBySharePreferencesUtil handleDataBySharePreferencesUtil;

    private SleepTimeHandleDataFromBleReceiver sleepTimeHandleDataFromBleReceiver;

    private int preBikeLockTime;
    private int preBikeSleepTime;

    private int stLock = 5;
    private int stSleep = 5;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bs_adjust_sleep_time);

        initView();
        initViewListener();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //注册广播
        sleepTimeHandleDataFromBleReceiver = new SleepTimeHandleDataFromBleReceiver();
        registBroadcastReceiverAtActivity(sleepTimeHandleDataFromBleReceiver, BluetoothDeviceConstant.GET_DATA_FROM_BLUETOOTH_BROADCAST_RECEIVER_ACTION);

        setViewContent();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //回收广播接收者
        unregisterReceiver(sleepTimeHandleDataFromBleReceiver);
    }

    /***********************************************************************************************/
    /**                                 初始化控件对象 监听 控件内容方法                             **/
    /***********************************************************************************************/

    private void initView() {
        iv_adjust_sleep_time_back = (ImageView) findViewById(R.id.iv_adjust_sleep_time_back);

        tv_sleep_time_lock_percent = (TextView) findViewById(R.id.tv_sleep_time_lock_percent);
        sb_sleep_time_lock = (SeekBar) findViewById(R.id.sb_sleep_time_lock);
        ll_sleep_time_sleep = (LinearLayout) findViewById(R.id.ll_sleep_time_sleep);
        tv_sleep_time_sleep_percent = (TextView) findViewById(R.id.tv_sleep_time_sleep_percent);
        sb_sleep_time_sleep = (SeekBar) findViewById(R.id.sb_sleep_time_sleep);

        handleDateByBleUtil = HandleDataByBLEUtil.getInstance(this, -1);
        handleDataBySharePreferencesUtil = HandleDataBySharePreferencesUtil.getInstance(this);
    }

    private void initViewListener() {
        //回退事件监听
        iv_adjust_sleep_time_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });

        //改变锁车休眠时间
        sb_sleep_time_lock.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_sleep_time_lock_percent.setText(progress == 0 ? "10min" : progress == 5 ? "Never" : progress * 15 + "min");

                //记录当前选择的锁车休眠时间
                stLock = (0 == seekBar.getProgress()) ? 10 : ((5 == seekBar.getProgress()) ? 0 : seekBar.getProgress() * 15);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                preBikeLockTime = seekBar.getProgress();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                handleDateByBleUtil.sendDataToDevice("s|4,s," + stLock + "," + stSleep);
            }
        });

        //改变控制器休眠时间
        sb_sleep_time_sleep.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tv_sleep_time_sleep_percent.setText(progress == 0 ? "10min" : progress == 5 ? "Never" : progress * 15 + "min");

                //记录当前选择的控制器休眠时间
                stSleep = (0 == seekBar.getProgress()) ? 10 : ((5 == seekBar.getProgress()) ? 0 : seekBar.getProgress() * 15);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                preBikeSleepTime = seekBar.getProgress();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                handleDateByBleUtil.sendDataToDevice("s|4,s," + stLock + "," + stSleep);
            }
        });
    }

    private void setViewContent() {
        if(null != BlueToothAssist.connectDeviceName && BlueToothAssist.connectDeviceName.contains("vta")){
            //当前连接的是Volta
            ll_sleep_time_sleep.setVisibility(View.VISIBLE);
        }else {
            ll_sleep_time_sleep.setVisibility(View.GONE);
        }

        handleDateByBleUtil.sendDataToDevice("s|4,r");
    }

    /***********************************************************************************************/
    /**                                 自定义广播接收者类                                          **/
    /***********************************************************************************************/

    /**
     * 广播接收者类，负责接收从蓝牙发出的数据并对其进行处理
     */
    class SleepTimeHandleDataFromBleReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String result = intent.getStringExtra("result");

            //修改成功
            if (result.contains("^s|4,s")) {
                Toast.makeText(context.getApplicationContext(), ToastConstant.MODIFY_BIKE_DORMANT_TIME_SUCCESS, Toast.LENGTH_SHORT).show();
                return;
            }

            //初始化数据
            if (result.contains("^s|4,r")) {
                String[] outPut = result.replace("^s|4,r,", "").replace("$", "").split(",");
                Integer lockTime = Integer.parseInt(outPut.length > 0 ? outPut[0] : "0");
                Integer sleepTime = Integer.parseInt(outPut.length > 1 ? outPut[1] : "0");
                sb_sleep_time_lock.setProgress(0 == lockTime ? 5 : lockTime / 15);
                sb_sleep_time_sleep.setProgress(0 == sleepTime ? 5 : sleepTime / 15);
                return;
            }


            //修改失败 车休眠时间改至初始状态
            Toast.makeText(context.getApplicationContext(), ToastConstant.MODIFY_BIKE_DORMANT_TIME_FAILURE, Toast.LENGTH_SHORT).show();
            sb_sleep_time_lock.setProgress(preBikeLockTime);
            sb_sleep_time_sleep.setProgress(preBikeSleepTime);
        }
    }
}
